<script setup lang="ts">
import { getIndividualApi, updateIndividualApi, updatePhotolApi } from '@/apis/userInfo';
import { useUserStore } from '@/stores';
import type { UploadRequestOptions } from 'element-plus';
import { Plus } from '@element-plus/icons-vue'
import type { UserIndividualType } from '@/types/userInfo';
import { ref } from 'vue';

// 表单数据
const individualForm = ref<UserIndividualType>({
  id: '',
  name: '',
  photo: null,
  mobile: '',
  gender: 0,
  birthday: '',
  intro: '',
})

// 校验规则
const rules = {
  name: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
  ],
  mobile: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
  ],
  gender: [
    { required: true, message: '请选择性别', trigger: 'change' }
  ],
  birthday: [
    { required: true, message: '请选择生日', trigger: 'change' }
  ]
};
// 获取用户信息
const getUserInfo = async () => {
  const res = await getIndividualApi()
  useUserStore().setIndividual(res.data.data)
  individualForm.value = useUserStore().individual!
}
getUserInfo()

// 上传头像
const uploadAction = async (options: UploadRequestOptions) => {
  const file = options.file // 获取文件对象
  // 校验图片格式
  const isImage = file.type.startsWith('image/')
  if (!isImage) {
    ElMessage.error('只能上传图片文件!');
    return false;
  }
  // 上传图片
  const fd = new FormData()
  fd.append('photo', file)

  const res = await updatePhotolApi(fd)
  individualForm.value.photo = res.data.data.photo
}

// 修改个人资料
const profileIndividual = async () => {
  await updateIndividualApi(individualForm.value)
  ElMessage({
    message: '修改成功',
    type: 'success'
  })
  // 获取最新数据
  getUserInfo()
}
</script>

<template>
  <div class="individual-container">
    <el-card class="card" shadow="always">
      <!-- 标题 -->
      <template v-slot:header>
        <div>
          <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item to="/overview">首页</el-breadcrumb-item>
            <el-breadcrumb-item>个人中心</el-breadcrumb-item>
          </el-breadcrumb>
        </div>
      </template>
      <el-form ref="form" :model="individualForm" :rules="rules" label-width="100px" label-position="top">
        <el-row :gutter="20">
          <el-col :span="10">
            <!-- 用户名 -->
            <el-form-item label="用户名" prop="name">
              <el-input v-model="individualForm.name"></el-input>
            </el-form-item>
            <!-- 手机号 -->
            <el-form-item label="手机号" prop="mobile">
              <el-input v-model="individualForm.mobile"></el-input>
            </el-form-item>
            <!-- 性别 -->
            <el-form-item label="性别" prop="gender">
              <el-select v-model="individualForm.gender" placeholder="请选择性别">
                <el-option label="男" :value="0"></el-option>
                <el-option label="女" :value="1"></el-option>
              </el-select>
            </el-form-item>
            <!-- 生日 -->
            <el-form-item label="生日" prop="birthday">
              <el-date-picker
                v-model="individualForm.birthday"
                type="date"
                placeholder="选择日期"
                format="YYYY-MM-DD"
                value-format="YYYY-MM-DD"
              ></el-date-picker>
            </el-form-item>
            <!-- 提交按钮 -->
            <el-form-item>
              <el-button @click="profileIndividual()" style="width: 90px;" type="primary">保存</el-button>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!-- 头像 -->
            <el-form-item label="头像">
              <el-upload
                class="avatar-uploader"
                :show-file-list="false"
                :http-request="uploadAction"
              >
                <img v-if="individualForm.photo" :src="individualForm.photo" class="avatar" />
                <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
              </el-upload>
              <div class="hm-text">零基础 学IT 就来—黑马 程序员！</div>
            </el-form-item>
            <el-form-item label="个人简介">
              <!-- 个人简介 -->
              <el-input type="textarea" :rows="4" placeholder="请输入内容" v-model="individualForm.intro"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
  </div>
</template>

<style scoped>
.hm-text {
  position: absolute;
  left: 250px;
  width: 240px;
  height: 170px;
  font-size: 40px;
  border-radius: 10px;
  font-weight: 800;
  line-height: 50px;
  display: flex;
  align-items: center;
  background-color: #fff;
  color: #002233;
  text-shadow: 1px 1px 2px #fff, -1px -1px 2px #000;
}
.individual-container {
  padding: 65px 150px;
  background-color: #f5f5f5;
  width: 100%;
  height: 100%;
}

.card {
  border-radius: 20px;
}

.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}

.el-form-item {
  margin-bottom: 20px;
}

.el-button {
  width: 100%;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>